﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CuboidDrawer : AbstractShapeDrawer
{
    private float length = 0;
    private float weight = 0;
    private float hight = 0;

    private float lOffset = 0;
    private float wOffset = 0;
    private float hOffset = 0;
    private BoxCollider boxCollider;

    protected override void OnEnable()
    {
        base.OnEnable();
        
    }
    public void SetBoxCollider()
    {
        if (boxCollider == null)
        {
            GameObject obj = new GameObject();
            obj.name = "ModelColliderEditorObject";
            obj.transform.SetParent(transform.parent);
            TransformUtility.ResetTransform(obj.transform);
            boxCollider = obj.AddComponent<BoxCollider>();
        }
    }

    /// <summary>
    /// x,y,z 
    /// </summary>
    /// <param name="length">z</param>
    /// <param name="weight">x</param>
    /// <param name="hight">y</param>
    public void InitDrawCube(float weight, float hight, float length,  float wOffset , float hOffset, float lOffset)
    {
        //TODO reDrawer 
        FucTempLog.LogWhite(string.Format("刷新包围盒长高宽：{0},{1},{2}", length, hight, weight));
        if (this.length != length || this.weight != weight || this.hight != hight)
        {
            this.length = length;
            this.weight = weight;
            this.hight = hight;

            //绘制矩形
            DrawCube(weight, hight, length);//x,z,y
            boxCollider.size = new Vector3(weight, hight, length);
        }
        if (this.lOffset != lOffset || this.wOffset != wOffset || this.hOffset != hOffset)
        {
            this.lOffset = lOffset;
            this.wOffset = wOffset;
            this.hOffset = hOffset;
            //设置BoxCollider
            boxCollider.center = new Vector3(wOffset, hOffset, lOffset);
            //设置偏移
            gameObject.transform.localPosition = new Vector3(wOffset, hOffset, lOffset);
        }
    }

    /// <summary>
    /// 根据绘制器上的BoxCollider绘制，并保存是数据
    /// </summary>
    /// <param name="groupData"></param>
    public Vector3[] RefreshCubeData(Data.ActionGroup groupData)
    {
        Vector3[] arr = new Vector3[2];
        var size = boxCollider.bounds.size;
        var center = boxCollider.center;
        //保存配置数据
        groupData.BoundingLength =Mathf.CeilToInt( size.z * 1000);
        groupData.BoundingWidth = Mathf.CeilToInt(size.x * 1000);
        groupData.BoundingHeight = Mathf.CeilToInt(size.y * 1000);

        groupData.BoundingLengthOffset = Mathf.CeilToInt(center.z * 1000);
        groupData.BoundingWidthOffset = Mathf.CeilToInt(center.x * 1000);
        groupData.BoundingHeightOffset = Mathf.CeilToInt(center.y * 1000);

        gameObject.transform.localPosition = boxCollider.center;
        DrawCube(size.x, size.y, size.z);

        arr[0] =  new Vector3(groupData.BoundingWidth,groupData.BoundingHeight, groupData.BoundingLength);
        arr[1] = new Vector3( groupData.BoundingWidthOffset, groupData.BoundingHeightOffset, groupData.BoundingLengthOffset);
        return arr;
    }
    public void UpdateScale(Vector3 scaleOffset)
    {

    }
    public void UpdateLocalPos()
    {

    }
}
